<template>
	<view class="page">
		<view class="common-wrap tp-w690">
			<view class="input-wrap">
				<view class="type">姓名</view>
				<view class="input">
					<input type="text" v-model="updata.name" placeholder="请输入姓名" />
				</view>
			</view>
			<view class="input-wrap">
				<view class="type">性别</view>
				<picker :range="genderList" @change="handleChooseGender">
					<view class="input">
						<input type="text" disabled v-model="updata.gender" placeholder="请选择" />
						<uni-icons type="arrowright" size="20" color="#ccc"></uni-icons>
					</view>
				</picker>
			</view>
			<view class="input-wrap">
				<view class="type">身份类型 <uni-icons @click="goToFWb(2,'类型说明')" style="margin-left: 10rpx;" type="help" size="18"
					 color="#FFE594"></uni-icons>
				</view>
				<picker mode="selector" :range="riderTyperList" range-key="typeName" @change="handleChooseRiderType">
					<view class="input">
						<input type="text" disabled v-model="updata.fareTypeName" placeholder="请选择" />
						<uni-icons type="arrowright" size="20" color="#999"></uni-icons>
					</view>
				</picker>
			</view>
			<view class="input-wrap">
				<view class="type">证件名称</view>
				<picker :range="IDTypeList" :range-key="'name'"  @change="handleChooseIDType">
					<view class="input">
						<input type="text" disabled v-model="updata.IDType" placeholder="请选择" />
						<uni-icons type="arrowright" size="20" color="#ccc"></uni-icons>
					</view>
				</picker>
			</view>
			<view class="input-wrap">
				<view class="type">证件号码</view>
				<view class="input">
					<input type="text" v-model="updata.ID"  placeholder="请输入证件号码" />
				</view>
			</view>
			<!-- <view class="input-wrap">
				<view class="type">证件过期时间</view>
				<picker mode='date' @change="handleChooseDate" :start="startTime" :end="endTime">
					<view class="input">
						<input type="text" disabled v-model="updata.expireTime" placeholder="请选择" />
						<uni-icons type="arrowright" size="20" color="#ccc"></uni-icons>
					</view>
				</picker>
			</view> -->
			<!-- <view class="input-wrap">
				<view class="type">手机号</view>
				<view class="input">
					<input type="number" v-model="updata.phone" placeholder="请输入手机号"/>
				</view>
			</view> -->

			<!-- 身份证图片 S -->
			<!--<view class="ID-wrap">
				<view class="type">身份证</view>
				<view class="ID_content">
					<view class="ID_positive ID" @click="handleUploadIDImgPositive('IDImgPositive')">
						<view class="bg-wrap">
							<view class="camer-icon-wrap">
								<image src="@/static/imgs/icons/camera-icon.png"></image>
							</view>
							<text>上传身份证正面</text>
						</view>
						<image class="ID_img" :src="updata.IDImgPositive" mode="scaleToFill"></image>
					</view>
					<view class="ID_negative ID" @click="handleUploadIDImgPositive('IDImgNegative')">
						<view class="bg-wrap">
							<view class="camer-icon-wrap">
								<image src="@/static/imgs/icons/camera-icon.png"></image>
							</view>
							<text>上传身份证反面</text>
						</view>
						<image class="ID_img" :src="updata.IDImgNegative" mode="scaleToFill"></image>
					</view>
					 <view class="ID_handheld ID" @click="handleUploadIDImgPositive('IDImgHandheld')">
						<view class="bg-wrap">
							<view class="camer-icon-wrap">
								<image src="@/static/imgs/icons/camera-icon.png"></image>
							</view>
							<text>手持身份证照片</text>
						</view>
						<image class="ID_img" :src="updata.IDImgHandheld" mode="scaleToFill"></image>
					</view> 
				</view>
			</view>-->
			<!-- 身份证图片 E -->
		</view>

		<view class="common-btn" @click="handleConfirmAddPilot">确定</view>

		<CommonAlert :config="alertConfig" ref="alert" @confirm="handleConfirmAlert"></CommonAlert>
	</view>
</template>

<script>
	import calendar from '@/components/tp-calendar/util.js'
	import CommonAlert from '../components/common/common-alert.vue'
	export default {
		components: {
			CommonAlert
		},
		data() {
			return {
				alertConfig: {
					cancleText: '我再想想',
					confirmText: '确认提交',
					content: '<p>请仔细核对填写的信息,</p><p>若因实名信息有误而影响行程,</p><p> 用户自行承担!</p>'
				},
				startTime: '', // 开始时间
				endTime: '', // 结束时间
				genderList: ['未知', '男', '女'],
				IDTypeList: ['身份证'],
				IDNameArrayList: [], // 证件名字
				riderTyperList: [], // 乘客类型picker(详细信息)
				updata: {
					name: '',
					gender: '',
					phone: '',
					IDType: '', // 证件类型
					fareType: '', // 身份类型
					fareTypeName: '', // 身份类型 名字
					ID: '', // 证件号码
					IDImgPositive: '', // 身份证正面照
					IDImgNegative: '', // 身份证反面照
					IDImgHandheld: '', // 身份证手持正面照
					driverLicense: '', // 驾驶证
					// expireTime: '', // 证件过期时间
				}
			};
		},
		onLoad() {
			const curDay = calendar.getDateObj() // 当前日期
			this.startTime = curDay.full
			this.endTime = (curDay.year + 20)  + '-' + curDay.month + '-' + curDay.day
			this.getRiderType() // 乘客身份类型选择
			this.getCardsType() // 乘客证件类型选择
		},
		methods: {
			// 上传图片
			handleUploadIDImgPositive(key) {
				const _this = this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera '],
					success: ({
						tempFilePaths
					}) => {
						uni.uploadFile({
							url: _this.$baseUrl + _this.$api.uploadFile,
							filePath: tempFilePaths[0],
							name: 'file',
							success: (uploadFileRes) => {
								let uploadData = JSON.parse(uploadFileRes.data)
								_this.$set(this.updata, key, uploadData.url)
							}
						})
					}
				});
			},
			handleConfirmAddPilot() {
				let regexID = this.$regexID
				let regexPhone = this.$regexPhone
				let {
					name,
					gender: sex,
					phone: mobile,
					IDType: certificateName,
					fareType,
					ID: certificateNumber,
					IDImgPositive: certificateFront,
					IDImgNegative: certificateBack,
					IDImgHandheld: certificateHandle,
					driverLicense,
					// expireTime
				} = this.updata
				if (!name.trim()) return uni.showToast({
					title: '请输入姓名',
					icon: 'none'
				});
				if (!sex.trim()) return uni.showToast({
					title: '请选择性别',
					icon: 'none'
				});
				if (!fareType) return uni.showToast({
					title: '请选择身份类型',
					icon: 'none'
				});
				if (!certificateName.trim()) return uni.showToast({
					title: '请选择证件类型',
					icon: 'none'
				});
				if (!certificateNumber.trim()) return uni.showToast({
					title: '请输入证件号码',
					icon: 'none'
				});
				if (certificateNumber.length < 6) return uni.showToast({
					title: '证件号不能低于6位',
					icon: 'none'
				});
				if (certificateName == '身份证') {
					if (!regexID.test(certificateNumber.trim())) return uni.showToast({
						title: '请检查输入的证件号码',
						icon: 'none'
					});
				}
				// if(!regexPhone.test(mobile.trim())) return uni.showToast({title: '请输入手机号',icon: 'none'});
				// if (!expireTime) return uni.showToast({
				// 	title: '请选择证件过期时间',
				// 	icon: 'none'
				// });
				// if(!certificateFront) return uni.showToast({title: '请上传身份证正面',icon: 'none'});
				// if(!certificateBack) return uni.showToast({title: '请上传身份证反面',icon: 'none'});
				// if(!certificateHandle) return uni.showToast({title: '请上传手持身份证照片',icon: 'none'});
				// if(!driverLicense) return uni.showToast({title: '请上传驾驶证照片',icon: 'none'});
				this.$refs.alert.show()

			},
			// 选择身份类型
			handleChooseRiderType(e) {
				const i = e.detail.value
				this.updata.fareTypeName = this.riderTyperList[i].typeName
				this.updata.fareType = this.riderTyperList[i].id
			},
			// 获取乘客类型
			getRiderType() {
				this.$http.get(this.$api.getRiderType).then(res => {
					if (res.code === 200) {
						this.riderTyperList = res.data
					}
					
				})
			},
			// 选择证件过期时间
			handleChooseDate(e) {
				this.updata.expireTime = e.detail.value
			},
			handleChooseGender(e) {
				let i = e.detail.value

				this.$set(this.updata, 'gender', this.genderList[i])
			},
			// 乘客证件类型选择
			getCardsType() {
				this.$http.get(this.$api.getCardsType).then(res => {
					if (res.code === 200) {
						this.IDTypeList = res.data
						res.data.forEach((item, i) => {
							this.IDNameArrayList[i] = item.name
							// console.log(this.info.IDNameArrayList);
						})
					}
				})
			},
			handleChooseIDType(e) {
				let i = e.detail.value
				this.$set(this.updata, 'IDType', this.IDTypeList[i].name)
			},
			// 确认提交
			handleConfirmAlert() {
				let {
					name,
					phone: mobile,
					IDType: certificateName,
					fareType,
					ID: certificateNumber,
					IDImgPositive: certificateFront,
					IDImgNegative: certificateBack,
					IDImgHandheld: certificateHandle,
					driverLicense,
					// expireTime
				} = this.updata
				let sex = this.updata.gender == '未知' ? 0 : this.updata.gender == '男' ? 1 : 2
				this.$http.post(this.$api.SubmitAuthorizeStatus, {
					name,
					sex,
					mobile,
					fareType,
					certificateName,
					certificateNumber,
					certificateFront,
					certificateBack,
					certificateHandle,
					// expireTime
				}).then(res => {
					if (res.code === 200) {
						uni.navigateBack({
							delta: 1
						});
					}
				})
			},
			getRandomInt(min, max) {
				min = Math.ceil(min);
				max = Math.floor(max);
				return Math.floor(Math.random() * (max - min)) + min; //不含最大值，含最小值
			},
			//去富文本页面
			goToFWb(type, name) {
				uni.navigateTo({
					url: '/pages/others/depositRule?type=' + type + '&name=' + name
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		/deep/.input-placeholder {
			font-size: 30rpx;
			font-weight: normal;
			color: #ccc;
		}
	}

	.common-wrap {
		border-radius: 12rpx;
		background-color: #fff;
		padding: 0 30rpx;
		box-sizing: border-box;
		margin-top: 20rpx;

		.input-wrap {
			height: 100rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 1px solid #ebebeb;
			font-size: 30rpx;

			&:last-child {
				border: none;
			}

			&.remark {
				height: 200rpx;
				align-items: flex-start;
				padding: 30rpx 0;
				box-sizing: border-box;

				.textarea {
					flex: 1;
					height: 160rpx;
				}
			}

			.l-wrap {
				display: flex;
				align-items: center;

				.icon {
					display: inline-block;
					height: 30rpx;
					width: 30rpx;
					margin-right: 20rpx;
				}

				.terms {
					color: $blue;
				}
			}

			.pirce {
				color: $red;
				padding-right: 10rpx;
			}

			.ul_service {
				display: flex;

				.li_service {
					display: flex;
					align-items: center;
					font-size: 30rpx;
					margin-left: 80rpx;

					.txt {
						margin-left: 20rpx;
					}
				}
			}

			.type {
				font-size: 30rpx;
			}

			.input {
				flex: 1;
				text-align: right;
				font-size: 30rpx;
				display: flex;

				input {
					padding-right: 6rpx;
				}
			}
		}

		.ID-wrap {
			.type {
				height: 100rpx;
				line-height: 100rpx;
				font-size: 30rpx;
			}

			.ID_content {
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;

				.ID {
					margin-bottom: 20rpx;
					position: relative;
					width: 296rpx;
					height: 188rpx;
					border: 1px dashed #ccc;
					border-radius: 12rpx;

					.bg-wrap {
						display: flex;
						flex-direction: column;
						align-items: center;
						justify-content: space-evenly;
						position: absolute;
						top: 0;
						left: 0;
						right: 0;
						bottom: 0;
						font-size: 26rpx;
						color: #ccc;
						z-index: 1;

						.camer-icon-wrap {
							height: 64rpx;
							width: 64rpx;
							border-radius: 50%;
							display: flex;
							justify-content: space-between;
							align-items: center;
						}
					}

					&.ID_handheld {
						width: 220rpx;
						height: 280rpx;

						.bg-wrap {
							padding: 50rpx 0;
							box-sizing: border-box;
						}
					}

					.ID_img {
						position: absolute;
						top: 0;
						left: 0;
						right: 0;
						bottom: 0;
						z-index: 2;
					}
				}
			}
		}
	}

	.common-btn {
		width: 580rpx;
		margin: 40rpx auto 58rpx;
		box-shadow: none;
	}
</style>
